#include "Cameras.h"
//Camera Control

Cameras::Cameras(){
}

void Cameras::setInitialCamera(){
	activeCamera = 0;	//bird
	slingShotBackPos = btVector3(50, 10, 0);
	birdBackCam = slingShotBackPos;
	heliCam = btVector3(50, 100, 100);
	freeCam = btVector3(50, 10, 0);
	birdBackCamLookAt = btVector3(50, 10, 60);	
	birdBackCamUp = btVector3(0, 1, 0);
	heliCamLookAt = btVector3(50, 0, 100);
	heliCamUp = btVector3(0, 0, 1);
	freeCamLookAt = btVector3(50, 0, 1000);
	freeCamUp = btVector3(0, 1, 0);
//	birdIsFlying = false;
	cameraLockedAtBird = false;
	timeStep = 1./15.f;
	lockY = 0.f;
	countTimeStep = 0;
}
	
btVector3 Cameras::getActiveCameraPos(){
	switch (activeCamera){
		default:
		case 0:	return birdBackCam;
		case 1:	return heliCam;
		case 2:	return freeCam;
	}
}

btVector3 Cameras::getActiveCameraLookAt(){
	switch (activeCamera){
		default:
		case 0:	return birdBackCamLookAt;
		case 1:	return heliCamLookAt;
		case 2:	return freeCamLookAt;
	}
}

btVector3 Cameras::getActiveCameraUp(){
	switch (activeCamera){
		default:
		case 0:	return birdBackCamUp;
		case 1:	return heliCamUp;
		case 2:	return freeCamUp;
	}
}

void Cameras::updateBirdBackCamera(btRigidBody* bird, int &flying, int &stopped){
	if (flying){
		if (stopped){
			if (countTimeStep < 720){
				countTimeStep++;
				return;
			}
			btVector3 temp = birdBackCam - slingShotBackPos;
//			temp *= timeStep;
//			printf("%lf %lf %lf\n", temp.x(), temp.y(), temp.z());
			temp.normalize();
			birdBackCam -= temp*timeStep*0.5;
			if (birdBackCam.z() <= 0){
				stopped = 0;
				flying = 0;
				cameraLockedAtBird = false;
				countTimeStep = 0;
			}
		}
		else if (cameraLockedAtBird){
			birdBackCam.setX(bird->getCenterOfMassPosition().x()+(bird->getCenterOfMassPosition().x()-birdBackCamLookAt.x()));
/*			if (bird->getCenterOfMassPosition().y() < lockY - 5){
				lockY = bird->getCenterOfMassPosition().y()-5;
			}*/
			birdBackCam.setY(bird->getCenterOfMassPosition().y()-(bird->getCenterOfMassPosition().y()-birdBackCamLookAt.y()));
			if (bird->getCenterOfMassPosition().z() < birdBackCamLookAt.z()){
				birdBackCam.setZ(bird->getCenterOfMassPosition().z()-7);
			}
			else{
				birdBackCam.setZ(bird->getCenterOfMassPosition().z()+10);
			}
//			if (birdBackCam.y() <= 5){
//				lockY++;
				//birdBackCam.setY(birdBackCam.y()+1);
//				birdBackCamLookAt.setY(birdBackCamLookAt.y()-1);
				//lockY = birdBackCam.y();
//			}
		}
		else{
			if (bird->getCenterOfMassPosition().z() >= birdBackCam.z() + 5){
				btVector3 temp = bird->getCenterOfMassPosition()-birdBackCam;
				temp.normalize();
				birdBackCam.setX(birdBackCam.x()+temp.x()/1/(15));
				birdBackCam.setZ(birdBackCam.z()+temp.z()/1/(15));
			}
			else{	
				btVector3 temp = bird->getCenterOfMassPosition()-birdBackCam;
				temp.normalize();
				birdBackCam.setX(birdBackCam.x()+temp.x()/1/(15));
				birdBackCam.setZ(birdBackCam.z()+temp.z()/1/(15));
				cameraLockedAtBird = true;
			}
		}	
	}
	else{
	}
}

void Cameras::moveForward(){
	freeCam.setZ(freeCam.z()+1);
}

void Cameras::moveBackward(){
	freeCam.setZ(freeCam.z()-1);
}

void Cameras::strafeRight(){
	freeCam.setX(freeCam.x()-1);
}

void Cameras::strafeLeft(){
	freeCam.setX(freeCam.x()+1);
}

void Cameras::lookUp(){
	freeCamUp.setY(freeCamUp.y()+1);
}

void Cameras::lookDown(){
	freeCamUp.setY(freeCamUp.y()-1);
}

void Cameras::lookRight(){
	freeCamUp.setX(freeCamUp.x()+1);
}

void Cameras::lookLeft(){
	freeCamUp.setX(freeCamUp.x()-1);
}

void Cameras::moveUp(){
	freeCam.setY(freeCam.y()+1);
	freeCamLookAt.setY(freeCamLookAt.y()+1);
}

void Cameras::moveDown(){
	freeCam.setY(freeCam.y()-1);
	freeCamLookAt.setY(freeCamLookAt.y()-1);
}

void Cameras::heliForward(){
	heliCam.setZ(heliCam.z()+1);
	heliCamLookAt.setZ(heliCamLookAt.z()+1);
}

void Cameras::heliBackward(){
	heliCam.setZ(heliCam.z()-1);
	heliCamLookAt.setZ(heliCamLookAt.z()-1);
}

void Cameras::heliRight(){
	heliCam.setX(heliCam.x()-1);
	heliCamLookAt.setX(heliCamLookAt.x()-1);
}

void Cameras::heliLeft(){
	heliCam.setX(heliCam.x()+1);
	heliCamLookAt.setX(heliCamLookAt.x()+1);
}

void Cameras::heliZoomIn(){
	heliCam.setY(heliCam.y()-1);
}
	
void Cameras::heliZoomOut(){
	heliCam.setY(heliCam.y()+1);
}
